/*
 * copyright (c) 2009 Alexandr [Sn@ble] Surnin
 *
 * This file is part of FrameSeeker.
 *
 * FrameSeeker is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FrameSeeker is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You can read the full version of GNU LGPL in
 * http://www.gnu.org/copyleft/lesser.html
 */


#include "FSFrame.h"

#include <QDebug>

// Static members declaration
QString FSFrame::imgFormat;
quint64 FSFrame::imgQuality;
quint64 FSFrame::imgWidth;


FSFrame::FSFrame() {

    painter = new QPainter();

}


FSFrame::FSFrame(uchar* vData,
                 quint64 _width,
                 quint64 _height,
                 quint64 _frameIndex,
                 quint64 _timeStamp,
                 quint64 _frameType) {

    new (this) FSFrame;

    createImage(vData, _width, _height);

    frameIndex = _frameIndex;
    timeStamp = _timeStamp;
    frameType = _frameType;

//    qDebug() << "FSFrame: index: " << frameIndex;
}


void FSFrame::createImage(uchar* vData,
                          quint64 _width,
                          quint64 _height) {

    if ((_height != height) | (_width != width)) {
        width = _width;
        height = _height;
        emit resized(width, height);
    }

    image = QImage(vData, width, height, QImage::Format_RGB32).copy(0, 0, width, height);

}




QImage * FSFrame::getImage() {

    return &image;
}



void FSFrame::saveImage(QString postfix) {

    // Create a original file name, like "/current/path/thumbnails/00012345.png"
    QString fileName = QDir::currentPath() + "/thumbnails/";
    fileName += QString::number(frameIndex, 10).rightJustified(8, '0', false);
    fileName += postfix;
    fileName += ".";
    fileName += FSFrame::imgFormat.toLower();


    // Save image
    if (FSFrame::imgWidth == 0) {
        FSFrame::imgWidth = image.width();
    }
    image.scaledToWidth(FSFrame::imgWidth).save(fileName, imgFormat.toLocal8Bit(), imgQuality);
    qDebug() << "Image " << fileName << " saved";
}


quint64 FSFrame::type() {

    return frameType;
}


quint64 FSFrame::index() {
    
    return frameIndex;
}


quint64 FSFrame::_timeStamp() {

    return timeStamp;
}


quint64 FSFrame::_width() {

    return width;
}


quint64 FSFrame::_height() {

    return height;
}


QImage FSFrame::_image() {

    return image;
}


void FSFrame::copy(FSFrame *frm) {

    if (!frm) return;

    frameIndex = frm->index();
    frameType = frm->type();
    width = frm->_width();
    height = frm->_height();
    timeStamp = frm->_timeStamp();
    image = frm->_image();

}


void FSFrame::readSettings(QSettings *settings) {

    FSFrame::imgFormat = settings->value("Image/Format").toString();
    FSFrame::imgQuality = settings->value("Image/Quality").toInt();
    FSFrame::imgWidth = settings->value("Image/Width").toInt();

    qDebug() << "Image Format: " << FSFrame::imgFormat
             << "\nImage Quality: " << FSFrame::imgQuality
             << "\nImage Width: " << FSFrame::imgWidth;
}
